package com.tuananh.radnom.model;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * 
 * @author SilentSkyTA This class symbolizes the connection between 2 NICs
 */

@XmlRootElement(name = "connection")
public class NetConn extends NetElement {
	/**
	 * A parent Nic is the one that is considered strong, according to WBEM
	 */
	NetInterface parent;
	/**
	 * In contrast with {@link #parent}
	 */
	NetInterface child;
	/**
	 * the type of connection , e.g : ethernet, optical, wireless
	 */
	String connType;

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		try {
			String name = parent.getName() + "--" + child.getName();
			return name;
		} catch (Exception e) {

		}
		return super.getName();
	}

	@XmlJavaTypeAdapter(NeXmlAdapter.class)
	public NetInterface getParent() {
		return parent;
	}

	public void setParent(NetInterface parent) {
		this.parent = parent;
	}

	@XmlJavaTypeAdapter(NeXmlAdapter.class)
	public NetInterface getChild() {
		return child;
	}

	public void setChild(NetInterface child) {
		this.child = child;
	}

	public String getConnType() {
		return connType;
	}

	public void setConnType(String connType) {
		this.connType = connType;
	}

	public boolean validate(Object ne) {
		// TODO Auto-generated method stub
		NetConn conn = (NetConn) ne;
		// A connection must have both parent and child netelement
		if (conn.getChild() == null || conn.getParent() == null)
			return false;
		// both endpoint of a connection must be NetInterface (Nic)
		if (!(conn.getChild() instanceof NetInterface)
				|| !(conn.getParent() instanceof NetInterface))
			return false;
		// The name of this connection must be the combination of parent and
		// child with "--" between
		if (!conn.getName().equals(
				conn.getParent().getName() + "--" + conn.getChild().getName()))
			return false;
		if (this.getName().equals(conn.getName()))
			return false;
		return true;
	}

	public boolean assign(Object obj) {
		// TODO Auto-generated method stub
		super.assign(obj);
		NetConn temp = (NetConn) obj;
		this.child.assign(temp.child);
		this.parent.assign(temp.parent);
		this.connType = temp.connType;
		return true;
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof NetConn){
			if (((NetConn)obj).getName().equals(this.getName()))
				return true;
		}
		return false;
	}
}
